<?php
/*
 * Class that operate on table 'tipo'. Database Mysql.
 *
 * @author: http://phpdao.com
 * @date: 2009-08-25 19:00
 */
class TipoDAO{

    /**
     * Get Domain object by primry key
     *
     * @param String $id primary key
     * @return TipoMySql
     */
    public function load($id){
            $sql = 'SELECT * FROM tipo WHERE codTipo = ?';
            $sqlQuery = new SqlQuery($sql);
            $sqlQuery->setNumber($id);
            return $this->getRow($sqlQuery);
    }

    public function getByNome($nome){
            $sql = 'SELECT * FROM tipo WHERE nomeTipo = ?';
            $sqlQuery = new SqlQuery($sql);
            $sqlQuery->setString($nome);
            return $this->getRow($sqlQuery);
    }

    /**
     * Get all records from table
     */
    public function queryAll(){
            $sql = 'SELECT * FROM tipo';
            $sqlQuery = new SqlQuery($sql);
            return $this->getList($sqlQuery);
    }

    /**
     * Get all records from table ordered by field
     *
     * @param $orderColumn column name
     */
    public function queryAllOrderBy($orderColumn){
            $sql = 'SELECT * FROM tipo ORDER BY '.$orderColumn;
            $sqlQuery = new SqlQuery($sql);
            return $this->getList($sqlQuery);
    }

    /**
     * Read row
     *
     * @return TipoMySql
     */
    protected function readRow($row){
            $tipo = new Tipo();

            $tipo->codTipo = $row['codTipo'];
            $tipo->nomeTipo = $row['nomeTipo'];
            $tipo->tagTipo = $row['tagTipo'];

            return $tipo;
    }

    protected function getList($sqlQuery){
            $tab = QueryExecutor::execute($sqlQuery);
            $ret = array();
            for($i=0;$i<count($tab);$i++){
                    $ret[$i] = $this->readRow($tab[$i]);
            }
            return $ret;
    }

    /**
     * Get row
     *
     * @return TipoMySql
     */
    protected function getRow($sqlQuery){
            $tab = QueryExecutor::execute($sqlQuery);
            return $this->readRow($tab[0]);
    }

    /**
     * Execute sql query
     */
    protected function execute($sqlQuery){
            return QueryExecutor::execute($sqlQuery);
    }


    /**
     * Execute sql query
     */
    protected function executeUpdate($sqlQuery){
            return QueryExecutor::executeUpdate($sqlQuery);
    }

    /**
     * Insert row to table
     */
    protected function executeInsert($sqlQuery){
            return QueryExecutor::executeInsert($sqlQuery);
    }
}
?>
